package com.framework.main.sys.attachment.action; 

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import com.framework.base.struts.BaseAction;
import com.framework.base.util.DateUtil;
import com.framework.base.util.FileUtil;
import com.framework.base.util.Struts2Utils;
import com.framework.main.sys.attachment.domain.AttachmentVo;
import com.framework.main.sys.user.domain.UserVo;

/**
 * Description:功能描述
 * tabaleName(对应的表名):
 * Company: www.51wuxia.com
 * @author：www.51wuxia.com
 * @version 1.0
 */
@SuppressWarnings("serial")
public class AttachmentAction extends BaseAction {
	//上传的附件
	private File[] attachment;
	private String[] attachmentContentType;
	private String[] attachmentFileName;
	private String[] userTypef;        //上传业务类型
	//下载附件
	private String downloadType; //下载类型
	private String downloadName; //下载文件名
	private String diskFileName; //硬盘名所在位置
	public InputStream downInputStream;//输出流来源

	public AttachmentAction() {
	}
	
	public String execute() throws Exception { 
		setDownloadName(java.net.URLEncoder.encode(getDownloadName(), "UTF-8"));
		return SUCCESS; 
	} 


	public File[] getAttachment() {
		return attachment;
	}
	public void setAttachment(File[] attachment) {
		this.attachment = attachment;
	}
	public String[] getAttachmentContentType() {
		return attachmentContentType;
	}
	public void setAttachmentContentType(String[] attachmentContentType) {
		this.attachmentContentType = attachmentContentType;
	}
	public String[] getAttachmentFileName() {
		return attachmentFileName;
	}
	public void setAttachmentFileName(String[] attachmentFileName) {
		this.attachmentFileName = attachmentFileName;
	}
	public String getDownloadType() {
		return downloadType;
	}
	public void setDownloadType(String downloadType) {
		this.downloadType = downloadType;
	}
	public String getDownloadName() {
		return downloadName;
	}
	public void setDownloadName(String downloadName) {
		this.downloadName = downloadName;
	}
	public String getDiskFileName() {
		return diskFileName;
	}
	public void setDiskFileName(String diskFileName) {
		this.diskFileName = diskFileName;
	}

	public String[] getUserTypef() {
		return userTypef;
	}

	public void setUserTypef(String[] userTypef) {
		this.userTypef = userTypef;
	}
	
	public InputStream getDownInputStream() {
		InputStream in=FileUtil.getFileInputStream(getDiskFileName());
		return in;
	}

	public void setDownInputStream(InputStream downInputStream) {
		this.downInputStream =downInputStream ;
	}
	
	public List<AttachmentVo> getAttachmentVoList(){
		List<AttachmentVo> attachmentVoList=new ArrayList<AttachmentVo>();
		UserVo currentUser=Struts2Utils.getCurrentUser();
		if(attachment!=null){
			for (int i=0;i<attachment.length;i++) {
				File file=(File)attachment[i];
				String fileType=attachmentContentType[i];
				AttachmentVo vo=new AttachmentVo();
				vo.setLastModified(DateUtil.getFullTime());
				vo.setFileNamef(attachmentFileName[i]);
				vo.setOperatorIdf(currentUser.getIdf());
				vo.setOperatorNamef(currentUser.getTrueNamef());
				vo.setFileTypef(fileType);
				vo.setFileSizef(file.length());
				vo.setFile(file);
				vo.setNewFile(true);
				vo.setUsetypef(Integer.parseInt(userTypef[i]));
				attachmentVoList.add(vo);
			}
		}
		return attachmentVoList;
	}
	/**
	 * 保存业务数据和附件数据
	 * @author lbx 
	 * @createTime 2011-06-18
	 * @return
	 * @throws Exception
	 */
	public String doInsertAttach() throws Exception{
		String resultName="";
		if(attachment!=null&&attachment.length>0){//判断是否有附件上传
			resultName = this.getBaseService().doInsert(this.getVo(),FileUtil.doUploadFile(getAttachmentVoList()));
		}else{
			resultName = this.getBaseService().doInsert(this.getVo());
		}
		return resultName;
	}
	
	public String doUpdateAttach() throws Exception{
		String resultName="";
		if(attachment!=null&&attachment.length>0){//判断是否有附件上传
			resultName = this.getBaseService().doUpdate(this.getVo(),FileUtil.doUploadFile(getAttachmentVoList()));
		}else{
			resultName = this.getBaseService().doUpdate(this.getVo());
		}
		return resultName;
	}

}
